package com.lebang.controller.sys;

import com.alibaba.fastjson.JSONObject;
import com.lebang.common.Login;
import com.lebang.domain.RankMember;
import com.lebang.entity.member.LbbMember;
import com.lebang.example.RankMemberExample;
import com.lebang.service.buss.MemberService;
import com.lebang.service.sys.RankMemberService;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;


/**
 * rank_member
 *
 * @author xwj
 * @date 2020-08-24
 */
@Controller
@RequestMapping("/sys/rankmember")
public class RankMemberController {
    @Autowired
    private RankMemberService baseService;
    @Autowired
    private MemberService memberService;

    @GetMapping("/list")
    public String list() {
        return "/rankmember/list";
    }

    @Login
    @GetMapping("/add")
    public String add(Integer id, Model model) {
        RankMember vo;
        if (id != null) {
            vo = baseService.selectById(id);
        } else {
            vo = new RankMember();
        }
        model.addAttribute("item", vo);
        return "/rankmember/edit";
    }


    @Login
    @PostMapping("/edit")
    @ResponseBody
    public JsonData edit(RankMember vo) {
        vo.setMemberId(vo.getMemberId().trim());
        if (vo.getId() != null) {
            vo.setModifyTime(LocalDateTime.now());
            baseService.updateById(vo);
        } else {
            vo.setCreateTime(LocalDateTime.now());
            vo.setHasDeleted("False");
            baseService.insertModel(vo);
        }
        return Callback.success("success", "修改成功");
    }

    @Login
    @PostMapping("/delete")
    @ResponseBody
    public JsonData delete(Integer id) {
        baseService.deleteById(id);
        return Callback.success("success", "删除成功");
    }

    @Login
    @ApiOperation(value = "分页")
    @PostMapping("/list")
    @ResponseBody
    public JSONObject list(HttpServletRequest request, @RequestParam Integer page, @RequestParam Integer limit) {
        RankMemberExample example = new RankMemberExample();
        example.setOffset((page - 1) * limit.longValue());
        example.setLimit(limit);
        RankMemberExample.Criteria criteria = example.createCriteria();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        List<Map<String,Object>> list = baseService.selectList((page - 1) * limit.longValue(),limit);
        jsonObject.put("data", list);
        jsonObject.put("count", baseService.countByExample(example));
        return jsonObject;
    }

    @Login
    @ApiOperation(value = "一键改名")
    @PostMapping("/updateName")
    @ResponseBody
    public JsonData updateName(){
        //所有虚假的账号
        List<RankMember> rankMemberList = baseService.selectByExample(null);
        rankMemberList.forEach(s->{
            LbbMember lbbMember = memberService.selectMemberById(s.getMemberId());
            List<String> list = memberService.selectMemberListName();
            int i = RandomUtils.nextInt(0,list.size()-1);
            lbbMember.setNickname(list.get(i));
            memberService.updateMemberById(lbbMember);
            s.setModifyTime(LocalDateTime.now());
            baseService.updateById(s);
        });
        return Callback.success("success", "改名成功");
    }


    public  String getPhoneNum() {
        String[] Top3 = {"133", "149", "153", "173", "177",
                "180", "181", "189", "199", "130", "131", "132",
                "145", "155", "156", "166", "171", "175", "176", "185", "186", "166", "134", "135",
                "136", "137", "138", "139", "147", "150", "151", "152", "157", "158", "159", "172",
                "178", "182", "183", "184", "187", "188", "198", "170", "171"};
        //随机出真实号段   使用数组的length属性，获得数组长度，
        //通过Math.random（）*数组长度获得数组下标，从而随机出前三位的号段
        String firstNum = Top3[(int) (Math.random() * Top3.length)];
        //随机出剩下的8位数
        String lastNum = "";
        final int last = 8;
        for (int i = 0; i < last; i++) {
            //每次循环都从0~9挑选一个随机数
            lastNum += (int) (Math.random() * 10);
        }
        //最终将号段和尾数连接起来
        String phoneNum = firstNum+ lastNum;

        phoneNum = "尾号" + phoneNum.substring(7);
        return phoneNum;

    }

}
